package com.muqi.app.qlearn.student;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.RadarChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.components.Legend.LegendPosition;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.RadarData;
import com.github.mikephil.charting.data.RadarDataSet;
import com.muqi.app.network.http.MyAsyncHttp;
import com.muqi.app.network.http.ParamsUtils;
import com.muqi.app.network.http.ResponseUtils;
import com.muqi.app.network.http.MyAsyncHttp.AsyncCallBackListener;
import com.muqi.app.network.http.MyAsyncHttp.RESULTCODE;
import com.muqi.app.project.contants.NetWorkApi;
import com.muqi.app.project.utils.NumberFormatUtil;
import com.muqi.app.qlearn.modles.GrowRewardAndMoveInfo;
import com.muqi.app.user.db.ChildBean;
import com.muqi.app.user.db.MyClassBean;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

public class CompetitionActivity extends BaseStudentActivity {

	private RadarChart mRadarChart;
	private TextView mTv_introduce;
	private TextView mTv_title;
	private TextView mTv_com_title;
	private List<GrowRewardAndMoveInfo> infos;
	private String selectedClassId;
	private String school_id;
	private String grade_id;
	private String student_id = "0";

	private String startTime;
	private String endTime;

	private int uiType;/* 0 综合竞争力报告 1 学业竞争力报告 */

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_competition);

	}

	@Override
	protected void onInit() {
		super.onInit();
		Intent intent = getIntent();
		if (intent != null) {
			uiType = intent.getIntExtra("uiType", 0);
		}

		mRadarChart = (RadarChart) findViewById(R.id.ward_chart);
		mTv_title = (TextView) findViewById(R.id.tv_title);
		mTv_com_title=(TextView) findViewById(R.id.tv_competition_title);
		
		mTv_title.setText(currentChildName + "竞争力报告");
		if (uiType==1) {
			mTv_com_title.setText("综合竞争力报告");
		}else{
			mTv_com_title.setText("学业竞争力报告");
		}
		
		SimpleDateFormat format = new SimpleDateFormat("yyyy.M.d");
		Calendar calendar = Calendar.getInstance();

		if (calendar.get(Calendar.MONTH) + 1 <= 9) {
			startTime = (calendar.get(Calendar.YEAR) - 1) + "-9-1";
			endTime = format.format(calendar.getTime());
		} else {
			startTime = (calendar.get(Calendar.YEAR)) + "-9-1";
			endTime = format.format(calendar.getTime());
		}

	}

	@Override
	protected void onData() {
		getMoveStatis();

	}

	/**
	 * 活动统计 综合素质
	 */
	private void getMoveStatis() {
		HashMap<String, Object> sendMap = new HashMap<String, Object>();
		sendMap.put("token", mSpUtil.getToken());
		sendMap.put("school_id", school_id);
		sendMap.put("grade_id", grade_id);
		sendMap.put("class_manager_id", selectedClassId);
		sendMap.put("student_id", student_id);
		sendMap.put("start_date", startTime);
		sendMap.put("end_date", endTime);
		String requestUrl = ParamsUtils.buildParams(
				NetWorkApi.GET_GROW_MOVE_STATISC, sendMap);

		MyAsyncHttp.get(this, requestUrl, new AsyncCallBackListener() {

			@Override
			public void onAsyncCallBack(RESULTCODE resuleCode, String request,
					String result) {
				if (resuleCode == RESULTCODE.SUCCESS) {
					// Log.e("===", result);
					infos = ResponseUtils.getGrowStatisticsInfos(mContext,
							result);
					if (infos != null) {
						showChart(mRadarChart, infos);
					}

				}

			}

		});

	}

	private void showChart(RadarChart mChart,
			List<GrowRewardAndMoveInfo> statisticsList2) {

		RadarData data = getPieData(statisticsList2);

		mChart.setDescription("");

		mChart.setRotationEnabled(false);

		mChart.setWebLineWidth(1.5f);
		mChart.setWebColor(Color.LTGRAY);
		mChart.setWebLineWidthInner(0.75f);
		mChart.setWebAlpha(100);
		mChart.setData(data);

		mChart.invalidate();

		mChart.animateXY(1400, 1400, Easing.EasingOption.EaseInOutQuad,
				Easing.EasingOption.EaseInOutQuad);

		XAxis xAxis = mChart.getXAxis();
		// xAxis.setTypeface(tf);
		xAxis.setTextSize(9f);

		YAxis yAxis = mChart.getYAxis();
		// yAxis.setTypeface(tf);
		yAxis.setLabelCount(6, true);
		yAxis.setTextSize(0f);
		yAxis.setTextColor(Color.parseColor("#00DEDEDE"));
		yAxis.setAxisMinValue(0f);

		Legend l = mChart.getLegend();
		l.setPosition(LegendPosition.LEFT_OF_CHART);
		// l.setTypeface(tf);
		l.setXEntrySpace(7f);
		l.setYEntrySpace(5f);
		mChart.animate();
	}

	/**
	 * 
	 * @param count
	 *            分成几部分
	 * @param range
	 */

	private RadarData getPieData(List<GrowRewardAndMoveInfo> statisticsList) {

		ArrayList<String> xValues = new ArrayList<String>(); // xVals用来表示每个饼块上的内容
		ArrayList<Entry> yValues = new ArrayList<Entry>(); // yVals用来表示封装每个饼块的实际数据
		ArrayList<Entry> yValues2 = new ArrayList<Entry>(); // yVals用来表示封装每个饼块的实际数据

		for (int i = 0; i < statisticsList.size(); i++) {

			/*
			 * if (data != null) { xValues.add(statisticsList.get(i).desc); //
			 * 饼块上显示成Quarterly1, // Quarterly2, // Quarterly3, // Quarterly4
			 * 
			 * yValues.add(new Entry(NumberFormatUtil
			 * .getFloatFromString(data.grade), i)); yValues2.add(new
			 * Entry(NumberFormatUtil .getFloatFromString(data.cls), i));
			 * 
			 * }
			 */

		}

		// y轴的集合
		RadarDataSet pieDataSet = new RadarDataSet(yValues, "年级获奖量");// "Quarterly Revenue 2014"/*显示在比例图上*/);
		pieDataSet.setColor(Color.rgb(232, 52, 40));
		// pieDataSet.setFillColor(ColorTemplate.VORDIPLOM_COLORS[0]);
		pieDataSet.setDrawFilled(true);
		pieDataSet.setLineWidth(2f);

		RadarDataSet pieDataSet2 = new RadarDataSet(yValues2, "班级获奖量");// "Quarterly Revenue 2014"/*显示在比例图上*/);
		ArrayList<Integer> colors = new ArrayList<Integer>();

		pieDataSet2.setColor(Color.rgb(40, 167, 225));
		// pieDataSet2.setFillColor(colors);
		pieDataSet2.setDrawFilled(true);
		pieDataSet2.setLineWidth(2f);

		ArrayList<RadarDataSet> sets = new ArrayList<RadarDataSet>();
		sets.add(pieDataSet);
		sets.add(pieDataSet2);

		RadarData data = new RadarData(xValues, sets);

		data.setValueTextSize(8f);

		return data;
	}

	public void onBack(View v) {
		this.finish();
	}

	@Override
	protected void onChoiceChildResult(ChildBean myChild) {
		// TODO Auto-generated method stub

	}

}
